<?php

namespace App\service;

use Elastic\Elasticsearch\ClientBuilder;

/**
 * es封装
 * 候照辉
 * 2022.11.14
 */

class es
{
    /**
     * 构造函数
     *
     */
    public function __construct(){
        $this->client = ClientBuilder::create()->setHosts(['127.0.0.1'])->build();
    }

    /**
     * 索引一个函数
     */
    public function index($index,$type,$id,$body)
    {
        $params = [
            'index' => $index,
            'type' => $type,
            'id' => $id,
            'body' =>$body
        ];

        $response = $this->client->index($params);
        return ($response);
    }

    /**
     * 搜索
     */
    public function search($index,$type,$body){
        $params = [
            'index' => $index,
            'type' => $type,
            'body' => [
                'query' => [
                    'match' => [
                        'address' => $body
                    ]
                ],
                'highlight'=>[
                    'pre_tags'=>["<span style='color: red'>"],
                    'post_tags'=>["</span>"],
                    'fields'=>[
                        "goods_name"=>new \stdClass()
                    ]
                ]
            ]
        ];

        $response = $this->client->search($params);
        return($response);
    }
}
